package com.example.transfer2;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

import android.os.Environment;

class transferfileClient
{
	static Socket ClientSoc;
	static DataInputStream din;
	static DataOutputStream dout;
	static BufferedReader br;

	transferfileClient(Socket soc, String user, String password)
	{
		try
		{
			ClientSoc=soc;
			din=new DataInputStream(ClientSoc.getInputStream());
			dout=new DataOutputStream(ClientSoc.getOutputStream());
			br=new BufferedReader(new InputStreamReader(System.in));
			dout.writeUTF("LOGIN");
			dout.writeUTF(user);
			dout.writeUTF(password);
			String msgFromServer=din.readUTF();
			if (msgFromServer.compareTo("login successful")==0){}
			else if (msgFromServer.compareTo("Invalid username or password")==0){disconnect();}
		}
		catch(Exception ex) {}        
	}
	static boolean SendFile(String filename) throws Exception
	{        
		dout.writeUTF("SEND");
		File root = new File(Environment.getExternalStorageDirectory(), "Transfer2_files");
		if (!root.exists()) {
			root.mkdirs();
			return false;
		}

		File f = new File(root, filename);
		if(!f.exists())
		{
			System.out.println("File not Exists...");
			dout.writeUTF("File not found");
			return false;
		}

		dout.writeUTF(filename);

		String msgFromServer=din.readUTF();
		if(msgFromServer.compareTo("File Already Exists")==0)
		{
			String Option;
			System.out.println("File Already Exists. Want to OverWrite (Y/N) ?");
			Option=br.readLine();            
			if(Option=="Y")    
			{
				dout.writeUTF("Y");
			}
			else
			{
				dout.writeUTF("N");
				return false;
			}
		}
		else if(msgFromServer.compareTo("Server stopped")==0){
			disconnect();
			return false;}//TODO server has quit
		else if(msgFromServer.compareTo("SendFile")==0){
			FileInputStream fin=new FileInputStream(f);
			int ch;
			do
			{
				ch=fin.read();
				dout.writeUTF(String.valueOf(ch));
			}
			while(ch!=-1);
			fin.close();
			return true;
		}
		return false;
	}

	public static boolean ReceiveFile(String filename) throws Exception
	{
		dout.writeUTF("GET");
		dout.writeUTF(filename);
		String msgFromServer=din.readUTF();

		if(msgFromServer.compareTo("File Not Found")==0) {return false;}
		else if(msgFromServer.compareTo("Server stopped")==0){
			disconnect();
			return false;}//TODO server has quit
		else if(msgFromServer.compareTo("READY")==0)
		{
			File root = new File(Environment.getExternalStorageDirectory(), "Transfer2_files");
			if (!root.exists()) {
				root.mkdirs();
			}

			File f = new File(root, filename);
			/*            if(f.exists())
            {
                String Option;
                System.out.println("File Already Exists. Want to OverWrite (Y/N) ?");
                Option=br.readLine();            
                if(Option=="N")    
                {
                    dout.flush();
                    return false;   
                }                
            }*/

			FileOutputStream fout=new FileOutputStream(f);
			int ch;
			String temp;
			do
			{
				temp=din.readUTF();
				ch=Integer.parseInt(temp);
				if(ch!=-1)
				{
					fout.write(ch);                    
				}
			}while(ch!=-1);
			fout.close();
			return true;
		}
		return false;
	}

	public static void disconnect(){
		try {
			dout.writeUTF("DISCONNECT");
			ClientSoc.close();
			System.exit(1);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}